package org.itboys.mdl.controller.house;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mdl.conmon.Finals;
import org.itboys.mdl.entity.mongo.house.MdlHouse;
import org.itboys.mdl.service.mongo.house.MdlHouseService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：zzq
 * 时间：2017/11/30
 * 描述：迈迪龙_户型
 */
@Controller
@RequestMapping("/mdl/web/house")
public class MdlHouseController extends BaseController{
    @Autowired
    private MdlHouseService houseService;
    /**
     * 主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(
            HttpServletRequest request,
            Model model){

        return "/maidilong/house/index";
    }
    /**
     * 详情页
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            @RequestParam(value="id",required=true)Long id,
            Model model){

        MdlHouse house = houseService.findOne(id);
        model.addAttribute("house", house);

        return "/maidilong/house/input";
    }

    /**
     * 查询
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response){
        try{
            PageResult<MdlHouse> result=houseService.selectHouse(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 新增或者修改
     * @param request
     * @param response
     * @param house
     */
    @RequestMapping("/save")
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @ModelAttribute MdlHouse house,
                     @RequestParam(value="imageFile",required=false) MultipartFile imageFile,
                     @RequestParam(value="livingRoomFile",required=false) MultipartFile livingRoomFile,
                     @RequestParam(value="masterBedRoomFile",required=false) MultipartFile masterBedRoomFile,
                     @RequestParam(value="secondBedRoomFile",required=false) MultipartFile secondBedRoomFile,
                     @RequestParam(value="bookRoomFile",required=false) MultipartFile bookRoomFile,
                     @RequestParam(value="kitchenFile",required=false) MultipartFile kitchenFile){
        try{
            houseService.saveHouse(house,imageFile,livingRoomFile,masterBedRoomFile,secondBedRoomFile,bookRoomFile,kitchenFile);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        }catch (Exception e){
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErroException")){
                msg=e.getMessage();
            }else{
                msg= Finals.ERROMSG;
                logger.error("/mdl/web/house/save/------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 设置推荐
     * @param request
     * @param response
     * @param id
     * @param type
     */
    @RequestMapping("/setRecommend")
    public  void setRecommend(HttpServletRequest request,
                              HttpServletResponse response,
                              @RequestParam(value="id",required=false) Long id,
                              @RequestParam(value="type",required=false) Integer type

    ) {
        try {
            houseService.setRecommend(id,type);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/mdl/web/house/setRecommend------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));;
        }
    }
    /**
     * 删除
     * @param request
     * @param response
     * @param houseId
     */
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="houseId",required=true) Long houseId){
        try {
            houseService.deleteHouse(houseId);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        }catch (Exception e){
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = Finals.ERROMSG;
                logger.error("/mdl/web/house/delete------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 选择器
     */
    @RequestMapping("/choose" )
    public  String choose(HttpServletRequest request,
                          @RequestParam(value="isSingle",required=true)Integer isSingle,//是否单选
                          @RequestParam(value="param",required=false)String param,//查询参数
                          @RequestParam(value="callBack",required=false)String callBack,//制定的回掉函数
                          Model model){
        model.addAttribute("param",param);
        model.addAttribute("callBack",callBack);
        if(isSingle==null){
            return "/maidilong/house/choose/choose_erro";
        }else if(isSingle==1){
            return "/maidilong/house/choose/choose_single";
        }else if(isSingle==0){
            return "/maidilong/house/choose/choose_muilt";
        }

        return "/maidilong/house/choose/choose_erro";
    }
}
